﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 易容酒店管理系统
{
    public partial class Backstage : Form
    {
        public Backstage()
        {
            InitializeComponent();
        }

        private void picReturn_Click(object sender, EventArgs e)
        {
            Main_function newzMain_function = new Main_function();
            newzMain_function.Show();
            newzMain_function.TopLevel = false;
            Method.frm.panel1.Controls.Clear();
            Method.frm.panel1.Controls.Add(newzMain_function);
        }
        //加载
        private void Backstage_Load(object sender, EventArgs e)
        {
            Look();
            Look2();
            Look3();
        }
        
        #region 添加客房种类
        //添加客房种类
        private void butADD_Click(object sender, EventArgs e)
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string Sql = "insert into Guest_Room values ('" + txtType1.Text + "','" + numNumber1.Value + "','" + numMoney1.Value + "');";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    int num = cmd.ExecuteNonQuery();
                    if (num > 0)
                    {
                        //MessageBox.Show("添加成功");
                    }
                }
            }
            Look();
            ADD();
        }
        //读取已录入的客房类型
        private void Look()
        {
            listView1.Items.Clear();
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            SqlConnection con = new SqlConnection(Link);
            string Sql = "select * from Guest_Room;";
            SqlDataAdapter Add = new SqlDataAdapter(Sql, con);
            DataSet set = new DataSet();
            Add.Fill(set, "Guest_Room");
            DataTable table = set.Tables["Guest_Room"];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow drow = table.Rows[i];
                ListViewItem lvi = new ListViewItem(drow["GuestType"].ToString());
                lvi.SubItems.Add(drow["Number"].ToString());
                lvi.SubItems.Add(drow["Money"].ToString());
                listView1.Items.Add(lvi);
            }
        }
        /// <summary>
        /// 对客房进行编号
        /// </summary>
        public void ADD()
        {
            int num1 = this.listView1.Items.Count;
            int num2 = 0;
            int num3 = 0;
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                num2 = (int)numNumber1.Value;
                string ID = "";
                for (int i = 1; i <= num2; i++)
                {
                    if (i < 10)
                    {
                        ID = num1.ToString() + "0" + i.ToString();
                    }
                    else
                    {
                        ID = num1.ToString() + i.ToString();
                    }
                    string Sql = "insert into GuestInfo values ('" + ID + "','" + txtType1.Text + "','无','已打扫')";
                    using (SqlCommand cmd = new SqlCommand(Sql, con))
                    {
                        con.Open();
                        num3 = cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
            if (num1 >= num2)
            {
                MessageBox.Show("添加成功");
            }
        }
        //删除客房类型
        private void butDelete_Click(object sender, EventArgs e)
        {
            int Index = 0;
            if (this.listView1.SelectedItems.Count > 0)//判断listview有被选中项
            {
                Index = this.listView1.SelectedItems[0].Index;//取当前选中项的index,SelectedItems[0]这必须为0
                String aa = listView1.Items[Index].SubItems[0].Text;//用我们刚取到的index取被选中的某一列的值从0开始
                Delete("Guest_Room", "GuestType", aa);
            }
            Look();
        }
        #endregion
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Ty">表名</param>
        /// <param name="lie">列名</param>
        /// <param name="Name">删除的名称</param>
        private void Delete(string Ty,string lie,string Name)
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string Sql = "Delete From "+Ty+" where "+lie+"='"+Name+"';";
                string Sql2 = "Delete From GuestInfo where Type='" + Name + "'";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    int num = cmd.ExecuteNonQuery();
                    if (num > 0)
                    {
                        MessageBox.Show("删除成功！");
                    }
                    con.Close();
                }
                using (SqlCommand cmd2 = new SqlCommand(Sql2,con))
                {
                    con.Open();
                    int num=cmd2.ExecuteNonQuery();
                    if (num > 0)
                    {
                        //
                    }
                }
            }
            
        }
        #region 添加物品名称
        //添加物品名称
        private void butADD2_Click(object sender, EventArgs e)
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string Sql = "insert into Goods values ('" + txtGoods.Text+ "','" + numNumber2.Value + "','" + numMoney2.Value + "');";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    int num = cmd.ExecuteNonQuery();
                    if (num > 0)
                    {
                        MessageBox.Show("添加成功");
                    }
                }
            }
            Look2();
        }
        //读取已录入的物品名称
        private void Look2()
        {
            listView2.Items.Clear();
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            SqlConnection con = new SqlConnection(Link);
            string Sql = "select * from Goods;";
            SqlDataAdapter Add = new SqlDataAdapter(Sql, con);
            DataSet set = new DataSet();
            Add.Fill(set, "Goods");
            DataTable table = set.Tables["Goods"];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow drow = table.Rows[i];
                ListViewItem lvi = new ListViewItem(drow["GoodsType"].ToString());
                lvi.SubItems.Add(drow["Number"].ToString());
                lvi.SubItems.Add(drow["Money"].ToString());
                listView2.Items.Add(lvi);
            }
        }
        //删除物品
        private void butDetele2_Click(object sender, EventArgs e)
        {
            int Index = 0;
            if (this.listView2.SelectedItems.Count > 0)//判断listview有被选中项
            {
                Index = this.listView2.SelectedItems[0].Index;//取当前选中项的index,SelectedItems[0]这必须为0
                String aa = listView2.Items[Index].SubItems[0].Text;//用我们刚取到的index取被选中的某一列的值从0开始
                Delete("Goods", "GoodsType", aa);
            }
            Look2();
        }
        #endregion
        #region 入住方式
        #region 免费房
        //开通
        private void radGratisNo_CheckedChanged(object sender, EventArgs e)
        {
            if (radGratisNo.Checked)
            {
                MessageBox.Show("免费房设定自动设定为5间");
                string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
                using (SqlConnection con = new SqlConnection(Link))
                {
                    string Sql = "insert into Method values ('免费房','0');";
                    using (SqlCommand cmd = new SqlCommand(Sql, con))
                    {
                        con.Open();
                        int num = cmd.ExecuteNonQuery();
                        if (num > 0)
                        {
                            //MessageBox.Show("添加成功");
                        }
                    }
                }
            }
            Look3();
        }
        //不开通
        private void radGratisOff_CheckedChanged(object sender, EventArgs e)
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string Sql = "Delete From Method where Method='免费房'";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    int num = cmd.ExecuteNonQuery();
                    if (num > 0)
                    {
                        //MessageBox.Show("添加成功");
                    }
                }
            }
            Look3();
        }
        #endregion
        //读取已录入的入住方式
        private void Look3()
        {
            listView3.Items.Clear();
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            SqlConnection con = new SqlConnection(Link);
            string Sql = "select * from Method;";
            SqlDataAdapter Add = new SqlDataAdapter(Sql, con);
            DataSet set = new DataSet();
            Add.Fill(set, "Method");
            DataTable table = set.Tables["Method"];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow drow = table.Rows[i];
                ListViewItem lvi = new ListViewItem(drow["Method"].ToString());
                lvi.SubItems.Add(drow["Money"].ToString());
                listView3.Items.Add(lvi);
            }
        }

        private void butADD3_Click(object sender, EventArgs e)
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string Sql = "insert into Method values ('" + txtGradist.Text+ "','" + numGradist.Value + "');";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    int num = cmd.ExecuteNonQuery();
                    if (num > 0)
                    {
                        MessageBox.Show("添加成功");
                    }
                }
            }
            Look3();
        }
        //删除
        private void butDelete3_Click(object sender, EventArgs e)
        {
            int Index = 0;
            if (this.listView3.SelectedItems.Count > 0)//判断listview有被选中项
            {
                Index = this.listView3.SelectedItems[0].Index;//取当前选中项的index,SelectedItems[0]这必须为0
                String aa = listView3.Items[Index].SubItems[0].Text;//用我们刚取到的index取被选中的某一列的值从0开始
                Delete("Method", "Method", aa);
            }
            Look3();
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            Main_function newzMain_function = new Main_function();
            newzMain_function.Show();
            newzMain_function.TopLevel = false;
            Method.frm.panel1.Controls.Clear();
            Method.frm.panel1.Controls.Add(newzMain_function);
            Method.SetDouble(Method.frm);
        }

        private void butEntry_Click(object sender, EventArgs e)
        {
            ADD newADD = new ADD();
            newADD.ShowDialog();
        }
    }
}
